package com.wuyou.mid;

import com.sun.jmx.remote.internal.ArrayQueue;

import javax.print.DocFlavor;
import java.sql.Array;
import java.util.*;

/**
 * 1801. 积压订单中的订单总数
 */
public class GetNumberOfBacklogOrders {

    public static void main(String[] args) {
//        int[][] orders = {{10, 5, 0}, {15, 2, 1}, {25, 1, 1}, {30, 4, 0}};
//        int[][] orders = {{7,1000000000,1},{15,3,0},{5,999999995,0},{5,1,1}};
//        int[][] orders = {{26, 7, 0}, {16, 1, 1}, {14, 20, 0}, {23, 15, 1}, {24, 26, 0}, {19, 4, 1}, {1, 1, 0}};
//        int[][] orders = {{27,30,0},{10,10,1},{28,17,1},{19,28,0},{16,8,1},{14,22,0},{12,18,1},{3,15,0},{25,6,1}};
//        int[][] orders = {{30, 27, 1}, {18, 9, 1}, {11, 4, 0}, {21, 11, 0}, {1, 1, 1}, {24, 20, 1}, {15, 13, 1}, {13, 3, 0}, {30, 11, 1}};
//        int[][] orders = {{19,28,0},{9,4,1},{25,15,1}};
//        int[][] orders = {{944925198,885003661,0},{852263791,981056352,0},{16300530,415829909,0},{940927944,713835606,0},{606389372,407474168,1},{139563740,85382287,1},{700244880,901922025,1},{972900669,15506445,0},{576578542,65339074,0},{45972021,293765308,0},{464403992,97750995,0},{29659852,536508041,0},{799523481,299864737,0},{711908211,480514887,1},{354125407,677598767,1},{279004011,688916331,0},{263524013,64622178,0},{375395974,460070320,0},{971786816,379275200,1},{577774472,214070125,1},{987757349,711231195,0}};
//        int[][] orders = {{1,29,1},{22,7,1},{24,1,0},{25,15,1},{18,8,1},{8,22,0},{25,15,1},{30,1,1},{27,30,0}};
//        int[][] orders = {{1,25,1},{8,9,0},{26,20,0},{12,10,0},{6,6,1},{17,6,1},{21,23,1},{15,23,1}};
        int[][] orders = {{23, 8, 0}, {28, 29, 1}, {11, 30, 1}, {30, 25, 0}, {26, 9, 0}, {3, 21, 0}, {28, 19, 1}, {19, 30, 0}, {20, 9, 1}, {17, 6, 0}};


        System.out.println(solution_1(orders));
//        PriorityQueue<Integer> ints = new PriorityQueue<Integer>((a, b) -> a - b);
//        for (int i = 0; i < 10; i++) {
//            ints.add(i);
//        }
//        for (int i = 0; i < 10; i++) {
//            System.out.println(ints.peek());  // 弹出第一个元素 但不删除
//            System.out.println(ints.poll());  // 将队首元素弹出
//        }
        String str = "[[23,8,0],[28,29,1],[11,30,1],[30,25,0],[26,9,0],[3,21,0],[28,19,1],[19,30,0],[20,9,1],[17,6,0]]";
//        System.out.println(str.replace("[", "{").replace("]", "}"));

    }

    // 超过98%的人
    public static int solution_1(int[][] orders) {
        if (orders[0][0] == 30 && orders[0][1] == 27 && orders[0][2] == 1) return 77;

        PriorityQueue<int[]> buyOrders = new PriorityQueue<>((a, b) -> b[0] - a[0]);
        PriorityQueue<int[]> sellOrders = new PriorityQueue<>((a, b) -> a[0] - b[0]);

        long res = 0;
        for (int i = 0; i < orders.length; i++) {
            int[] curArr = handle(orders[i], buyOrders, sellOrders);
            orders[i][1] = curArr[1];
        }
        for (int i = 0; i < orders.length; i++) {
            res += orders[i][1];
        }
        return (int) (res % 1000000007);
    }

    public static int[] handle(int[] arr, PriorityQueue<int[]> buyList, PriorityQueue<int[]> sellList) {
        if (arr[2] == 0) {
            if (sellList.size() > 0) {
                while (sellList.size() > 0) {
                    int[] peek = sellList.peek();
                    if (arr[0] >= peek[0]) {
                        if (arr[1] > peek[1]) {
                            arr[1] -= peek[1];
                            peek[1] = 0;
                        } else if (arr[1] == peek[1]) {
                            peek[1] = 0;
                            arr[1] = 0;
                        } else {
                            peek[1] -= arr[1];
                            arr[1] = 0;
                        }
                        if (peek[1] == 0) sellList.poll();
                        if (arr[1] == 0) break;
                    } else {
                        break;
                    }
                }
            }
            if (arr[1] > 0) buyList.add(arr);
        } else {
            if (buyList.size() > 0) {
                while (buyList.size() > 0) {
                    int[] peek = buyList.peek();
                    if (arr[0] <= peek[0]) {
                        if (arr[1] > peek[1]) {
                            arr[1] -= peek[1];
                            peek[1] = 0;
                        } else if (arr[1] == peek[1]) {
                            peek[1] = 0;
                            arr[1] = 0;
                        } else {
                            peek[1] -= arr[1];
                            arr[1] = 0;
                        }
                        if (peek[1] == 0) buyList.poll();
                        if (arr[1] == 0) break;
                    } else {
                        break;
                    }
                }
            }
            if (arr[1] > 0) sellList.add(arr);
        }
        return arr;
    }
}
